<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="full-screen" content="true" />
    <meta name="screen-orientation" content="portrait" />
    <meta name="x5-fullscreen" content="true" />
    <meta name="360-fullscreen" content="true" />
    <style>
        html, body {
            -ms-touch-action: none;
            background: #888888;
            padding: 0;
            border: 0;
            margin: 0;
            height: 100%;
        }
    </style>
</head>

<body>
    <div style="margin: auto;width: 100%;height: 100%;" class="egret-player"
         data-entry-class="Main"
         data-orientation="landscape"
         data-scale-mode="exactFit"
         data-frame-rate="30"
         data-content-width="1280"
         data-content-height="720"
         data-multi-fingered="2"
         data-show-fps="false" data-show-log="false"
         data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9">
    </div>

<script src="zlib.min.js"></script>
<script>
    function loadScript(text) {
        var script = document.createElement("script");
        script.setAttribute("type", "text/javascript");
        script.text = text;
        document.body.appendChild(script);
        document.body.removeChild(script);
    }

    function Utf8ArrayToStr(array) {
        var out, i, len, c;
        var char2, char3;
        out = "";
        len = array.length;
        i = 0;
        while(i < len) {
            c = array[i++];
            switch(c >> 4)
            { 
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                out += String.fromCharCode(c);
                break;
            case 12: case 13:
                // 110x xxxx   10xx xxxx
                char2 = array[i++];
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                char2 = array[i++];
                char3 = array[i++];
                out += String.fromCharCode(((c & 0x0F) << 12) |
                            ((char2 & 0x3F) << 6) |
                            ((char3 & 0x3F) << 0));
                break;
            }
        }
        return out;
    }

    try
    {
        // https://github.com/li9chuan/jscomp
        var xhr2 = new XMLHttpRequest();
        xhr2.responseType = "arraybuffer";
        //xhr2.open('GET', './game.cfg?v=' + Math.random(), true);
        xhr2.open('GET', './game.cfg', true);
        xhr2.addEventListener("load", function () {
            var dataview = new DataView(xhr2.response);
            var offset = 0;

            while( offset < dataview.byteLength ){
                var jscp_len  = dataview.getUint32(offset);

                if(jscp_len>0){
                    offset += 4;
                    var jscp_file = new Uint8Array( dataview.buffer, offset, jscp_len );
                    offset += jscp_len;

                    var inflate     = new Zlib.Inflate(jscp_file);
                    var js_file     = inflate.decompress();

                    //console.log( Utf8ArrayToStr(js_file) );
                    loadScript( Utf8ArrayToStr(js_file) );
                }
                else{
                    break;
                }
                console.log(jscp_len);
            }

            // loading completed, start game.
            /**
             * {
             * "renderMode":, //Engine rendering mode, "canvas" or "webgl"
             * "audioType": 0 //Use the audio type, 0: default, 2: web audio, 3: audio
             * "antialias": //Whether the anti-aliasing is enabled in WebGL mode, true: on, false: off, defaults to false
             * "calculateCanvasScaleFactor": //a function return canvas scale factor
             * }
             **/
            egret.runEgret({ renderMode: "webgl", audioType: 0, calculateCanvasScaleFactor:function(context) {
                var backingStore = context.backingStorePixelRatio ||
                    context.webkitBackingStorePixelRatio ||
                    context.mozBackingStorePixelRatio ||
                    context.msBackingStorePixelRatio ||
                    context.oBackingStorePixelRatio ||
                    context.backingStorePixelRatio || 1;
                return (window.devicePixelRatio || 1) / backingStore;
            }});
        });
        xhr2.send(null);
    }
    catch (e) {
        console.log("normal load.");
    }

</script>
</body>

</html>